package cn.ihoway.redis;

import cn.ihoway.dao.CharacterDao;
import cn.ihoway.entity.Character;
import cn.ihoway.util.HowayLog;
import cn.ihoway.util.HowayRedisCache;
import cn.ihoway.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class CharacterRedis {
    private HowayRedisCache cache = new HowayRedisCache(Character.class.getName());
    private final HowayLog logger = new HowayLog(CharacterRedis.class);

    public void init(){
        if(cache.getSize() == 0){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            CharacterDao characterDao = sqlSession.getMapper(CharacterDao.class);
            List<Character> list = characterDao.selectAll();
            for(Character character : list){
                cache.putObject(character.getId(),character);
            }
        }
    }

    public int size(){
        return cache.getSize();
    }

    public Character get(Integer id){
        return (Character) cache.getObject(id);
    }

}
